<?php
        class profile extends model {
           
           function view(){ 
           
           
          // echo "profile";
          
          

          $username=$this->info['parameter'][0];
          $sql="select userid,oauth from tb_user where username='".mysql_real_escape_string($username)."'";
          $arrayuser=$this->db->db_set_1recordrow($sql);
          if($username&&$arrayuser['oauth'])
          {
             $arraynewdata=array();           
             $arraynewdata=$this->memcache->get($arrayuser['oauth']);
             
             
           
           
             if(count($arraynewdata)==0||$_GET['nocahce']==1)
             {
              $arraynewdata=$this->getuseralldata($arrayuser['oauth'],',hideemail,hidephone,hidefax,hideetc,emailcontact');
              $this->memcache->set($arrayuser['oauth'],$arraynewdata);
             }
             else
             {
             
          $arraynewdata['userdata']['countproduct']=$this->countproduct($arraynewdata['userdata']['userid']);
          $arraynewdata['userdata']['countlikeproduct']=$this->countlikeproduct($arraynewdata['userdata']['userid']);
          $arraynewdata['userdata']['counttagproduct']=$this->counttagproduct($arraynewdata['userdata']['userid']);
          
          $arraynewdata['userdata']['countfollower']=$this->countfollower($arraynewdata['userdata']['userid']);
          $arraynewdata['userdata']['countfollowing']=$this->countfollowing($arraynewdata['userdata']['userid']);
             
             }
         $arraynewdata['data']['title']='Profile '.$arraynewdata['userdata']['displayname'];    
         $arraynewdata['data']['description']=$arraynewdata['userdata']['description'];    
         $arraynewdata['data']['link']=$arraynewdata['userdata']['username'];    
         $arraynewdata['data']['simg']=$arraynewdata['userdata']['simguser'];    
          
          if($this->userdata['oauth'])
          {
          $arraynewdata['userdata']['checkfollow']=$this->checkuserfollow($arraynewdata['userdata']['userid'],$this->userdata['oauth']);
          }else
          {
          $arraynewdata['userdata']['checkfollow']=false;
          
          }
          
            $arraynewdata['myscore']=$this->getmygamnification($arrayuser['userid']);

             $arraynewdata['badge']=$this->getbadgealldata($arrayuser['userid']);
             
         //    echo "<pre>";
//             print_r($arraynewdata['badge']);
//             echo "</pre>;";
//             exit;
             


          
          list($urinow,$urinow2)=explode("?",$_SERVER['REQUEST_URI']);
          $arraynewdata['urlnow']=trim($urinow);
          

          
          if(substr($arraynewdata['urlnow'], -1)=="/")
          {
          $arraynewdata['urlnow']= substr_replace($arraynewdata['urlnow'],"",-1);   
          }    
             
          
             
             
          $fid=0;
          $feature=array_pop(explode('/',$arraynewdata['urlnow']));
          $feature=trim($feature);

          if($feature)
          {
          $datafeature=$this->getfeaturebyname($feature);

          if(count($datafeature))
          {
          
          $arraynewdata['fid']=$datafeature['fid']; 
          $fid=$arraynewdata['fid'];
          $arraynewdata['featurename']=$datafeature['name'];
          
          $arraynewdata['urlnow']=str_replace("/".$feature,'',$arraynewdata['urlnow']);  
          
          
          }
          
          }
          
          $arraynewdata['templatefollow']=0;
          $arraynewdata['isuserpage']=1;
          
          
          if(strpos($arraynewdata['urlnow'],"/favorites")!==false)
          {
          $arraynewdata['templateuser']=2;
          $arraynewdata['fav']=$arraynewdata['userdata']['userid'];
          
          
          }
          else if(strpos($arraynewdata['urlnow'],"/tag")!==false)
          {
          
          $arraynewdata['tag']=$arraynewdata['userdata']['userid'];
          $arraynewdata['tagsearch']="";
          if(strpos($arraynewdata['urlnow'],"/tag/"))
          {
          list($a,$seachtext)=explode("/tag/",$arraynewdata['urlnow']);
          if($seachtext)
          {
          
          $seachtext= urldecode(str_replace("%20",' ',$seachtext)) ;
          $arraynewdata['tagsearch']=$seachtext;

          $arraynewdata['tagid'][]=$this->gettagidbytagname($seachtext);
          

          }
          
          }

          
          
          
          
          
          
          }
          else if(strpos($arraynewdata['urlnow'],"/followers")!==false)
          {
          $arraynewdata['templatefollow']=1;
          }
          else if(strpos($arraynewdata['urlnow'],"/following")!==false)
          {
          $arraynewdata['templatefollow']=2;
          }
          else if(strpos($arraynewdata['urlnow'],"/requests")!==false)
          {
          $arraynewdata['templatefollow']=3;
          }
          else if(strpos($arraynewdata['urlnow'],"/block")!==false)
          {
          $arraynewdata['templatefollow']=4;
          }
          else if(strpos($arraynewdata['urlnow'],"/blockfollowing")!==false)
          {
          $arraynewdata['templatefollow']=5;
          
          } 
          else if(strpos($arraynewdata['urlnow'],"/friendssuggestion")!==false)
          {
          
          
          
           if(count($this->userdata)!=0)
            {
            $sql='select facebookid,username from tb_user where oauth="'.$this->userdata['oauth'].'" ';
            $arraycheckf=$this->db->db_set_1recordrow($sql);
            
            if($arraycheckf['facebookid'])
            {
            $arraynewdata['fconnect']=1;
            require pluginpath.'/facebook/facebook.php';

            
            $facebook = new Facebook(array(
            'appId'  =>facebookappid,
            'secret' =>facebooksecret,
            ));
            
            
            $user = $facebook->getUser();

            
            if ($user) {
            try {
            // Proceed knowing you have a logged in user who's authenticated.
            $user_profile = $facebook->api('/me');
            } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
            }
            }
            

            
            if($user=='0')
            {
          
            
                $loginUrl=$facebook->getLoginUrl(
            array(
            'redirect_uri' => baseinfo.'/ajax/getloginfacebook?noredirect=1&nojson=1&linkto=/'.$username.'/friendssuggesion',
            'scope' => 'email,user_likes,user_photos,publish_stream,user_about_me,user_videos,status_update,user_birthday,user_location,user_work_history'));

            redirectto($loginUrl);
            
            
            
            }else
            {
            
            if($arraycheckf['facebookid']&&$user_profile['id'])
            {
            
            if($arraycheckf['facebookid']==$user_profile['id'])
            {
           
            
            
            }else
            {
            if($_GET['logoutfacebook'])
            {
              $loginUrl=$facebook->getLoginUrl(
            array(
            'redirect_uri' => baseinfo.'/ajax/getloginfacebook?noredirect=1&nojson=1&linkto=/'.$username.'/friendssuggesion',
            'scope' => 'email,user_likes,user_photos,publish_stream,user_about_me,user_videos,status_update,user_birthday,user_location,user_work_history'));

            redirectto($loginUrl);
            exit;
            
            }else
            {
            $facebook->destroySession();
            $logoutUrl=$facebook->getLogoutUrl(
            array(
            'next' => baseinfo.'/'.$arraycheckf['username'].'/friendssuggesion?logoutfacebook=1'));
            
           // echo "<pre>";
//            print_r($user);
//            echo "</pre>;";
//            echo $logoutUrl;
            redirectto($logoutUrl);
            exit;
            }
            

            }
            
            
            }
            
            $friendslist = $facebook->api('/me/friends');

            
            }
            
            }
            
            }
          
          $arraynewdata['templatesuggetion']=1;
          
          }
          else if(strpos($arraynewdata['urlnow'],"/messages")!==false)
          {
          $arraynewdata['msgto']="";
          if($this->userdata['userid'])
          {
          $arraynewdata['templatemessages']=1;
          if($_GET['userid'])
          {
          if($_GET['userid']!=$this->userdata['userid'])
          {
          $datausermsg=$this->getuserbyuserid($_GET['userid']);
          if($datausermsg['userid'])
          {
          
          $arraynewdata['msgto']=$datausermsg['userid'];
          $arraynewdata['msgstatus']=$this->setfollowother($datausermsg['userid'],$this->userdata['oauth']);
          

          
          }else
          {
          $arraynewdata=array();
          $this->loadtemplate('error',$arraynewdata);  
          exit;
          
          }
          
          
          }
          
          
          }
          
          }else
          {
          $arraynewdata=array();
          $this->loadtemplate('error',$arraynewdata);  
          exit;
          
          }
          
          
          
          }
          else
          {
          $arraynewdata['templateuser']=1;
          $arraynewdata['defaultprofile']=1;
          
          }
          
          if($_GET['myfeed'])
          {
          $arraynewdata['templateuser']=3;
          $arraynewdata['feed']=$this->userdata['userid'];
          
          }
          
          if($this->userdata['oauth']!=$arrayuser['oauth'])
          {
          
          $arraynewdata['templateuser']=0;
          $arraynewdata['isuserpage']=0;
          
          }
          
          $page=1;
          if($_GET['page'])
          {
          $page=$_GET['page'];

          }
          
          $arraynewdata['sitemap']=$this->getproductbyconditionshort($page,$arrayuser['userid'],$fid);
      


          $arraynewdata['cssdata'][]=baseinfo.'/css/theme1/profile.css';
          $arraynewdata['cssdata'][]=baseinfo.'/css/theme1/followmessage.css';
          $this->loadtemplate('profile',$arraynewdata);  
          }else
          {
          $arraynewdata=array();
          $this->loadtemplate('error',$arraynewdata);  
          
          
          }
           
           }
        
        
        }
?>
